package lg.minecraft.plugin.pokemonmenu.manager;

import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.config.MainConfig;
import lg.minecraft.plugin.pokemonmenu.util.pokemondata.StorageType;
import lg.minecraft.plugin.pokemonmenu.util.pokemondata.storage.FileStorage;
import lg.minecraft.plugin.pokemonmenu.util.pokemondata.storage.MYSQLStorage;
import lg.minecraft.plugin.pokemonmenu.util.pokemondata.storage.PokemonDataStorage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

/**
 * @author langle__
 * @version 1.0
 */
public class PokemonDataManager {

    public static final PokemonDataManager INSTANCE = new PokemonDataManager();

    private PokemonDataStorage storage;

    private PokemonDataManager() {}

    public void loadSettings() {
        MainConfig mainConfig = LGPokemonMenu.getMainConfig();
        StorageType pokemonCloudStorage = mainConfig.getPokemonCloudStorage();
        switch (pokemonCloudStorage) {
            case FILE -> storage = new FileStorage(mainConfig.getPokemonDataFolder());
            case MYSQL -> storage = new MYSQLStorage(mainConfig.getPokemonCloudMySqlUrl(), mainConfig.getPokemonCloudMySqlUser(), mainConfig.getPokemonCloudMySqlPassword());
            default -> throw new IllegalArgumentException("Invalid pokemon cloud storage type");
        }
    }

    @Nullable
    public Pokemon getPokemon(@NotNull UUID uuid) {
        return storage.getPokemon(uuid);
    }

    public boolean savePokemon(@NotNull UUID uuid, @NotNull Pokemon pokemon) {
        return storage.savePokemon(uuid, pokemon);
    }

    public boolean removePokemon(@NotNull UUID uuid) {
        return storage.removePokemon(uuid);
    }

    public void close() {
        if (storage != null && storage instanceof MYSQLStorage) {
            ((MYSQLStorage) storage).close();
        }
    }

}
